// pages/index/home/bookinghome/bookinghome.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    homeId:'',
    userInfo:{},
    appoint:{},
    mode: '',
    dateVisible: false,
    date: new Date('2021-12-23').getTime(), // 支持时间戳传入
    dateText: '',

    // 指定选择区间起始值
    start: '2000-01-01 00:00:00',
    end: '2030-09-09 12:12:12',
  },
  loginHandle(e){
    console.log("one:"+this.data.homeId)
    console.log("two:"+this.data.userInfo.userId)
    console.log('表单提交了提交',e.detail.value.uaername);
    // console.log("123"+this.data.appoint)
    this.setData({
      appoint:{
        houseId:this.data.homeId,
        userId:this.data.userInfo.userId,
        appointTime:this.data.date,
        name:e.detail.value.uaername,
        contact:e.detail.value.phone
      },
    }),
    console.log("appoint对象是:"+this.data.appoint)
    wx.request({
      url:'http://localhost:8080/biz/appoint',
      method:'post',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data: JSON.stringify(this.data.appoint),
      success:(resp)=>{

        if(resp.code == 200){
          wx.showToast({
            title: '预约成功',
            icon:'success',
            duration: 2000
          })

          // wx.switchTab({
          //   url: '/pages/index/index',
          // })
        }else{
          wx.showToast({
            title: '你已预约',
            icon:'error',
            duration:2000
          })
          // wx.switchTab({
          //   url: '/pages/index/index',
          // })
        }
        //console.log(resp.data)
        setTimeout(()=>{
          wx.switchTab({
            url: '/pages/index/index',
          })
        },1000)

        // wx.switchTab({
        //   url: '/pages/index/index',
        // })
      },
      
    })
  },
  showPicker(e) {
    const { mode } = e.currentTarget.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },
  hidePicker() {
    const { mode } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
  onConfirm(e) {
    const { value } = e.detail;
    const { mode } = this.data;

    console.log('confirm', value);

    this.setData({
      [mode]: value,
      [`${mode}Text`]: value,
    });

    this.hidePicker();
  },

  onColumnChange(e) {
    console.log('pick', e.detail.value);
  },
  haveSave(e) {
      wx.showToast({
        title: '已提交预约',
      });
      let k=this
      setTimeout(k.Tohome,2000)
  },
  Tohome:function(){
  wx.redirectTo({
    url: '../home',
  })
},



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options.homeId)
    // var homeId = options.homeId
    // var userInfo = wx.getStorageSync('userInfo')
    this.setData({
      homeId :options.homeId,
      userInfo : wx.getStorageSync('userInfo')
    })
    // console.log("one:"+this.data.homeId)
    // console.log("two:"+this.data.userInfo.userId)
    //console.log(wx.getStorageSync('userInfo'))

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})